home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 073 (1988-11-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 073 (1988-11-15)(Ossowski, Stefan)(DE)(PD).adf / RunBack / path.c < prev    next >
C/C++ Source or Header  |  1988-08-14  |  4KB  |  185 lines

  1. /* This code is taken largely from Carolyn Scheppner's "which.c" program
  2.  * from 11/87.  There were no copyright notices in the original file,
  3.  * so to the best of my knowledge it is in the Public Domain.
  4. */
  5.  
  6. #include <exec/types.h>
  7. #include <exec/memory.h>
  8. #include <libraries/dos.h>
  9. #include <libraries/dosextens.h>
  10.  
  11. #define SBUFSZ 256
  12. #define CBUFSZ 80
  13.  
  14. extern BOOL getPath();
  15. extern struct Task *FindTask();        /* To scare away warning msgs */
  16. extern VOID *AllocMem();
  17.     
  18. struct Path
  19.    {
  20.    BPTR  path_Next;
  21.    LONG  path_Lock;
  22.    };
  23.  
  24. char *FindIt(command)
  25. char *command;
  26. {
  27.    struct Process *proc;
  28.    struct CommandLineInterface *cli;
  29.    struct Path *path;
  30.    struct FileInfoBlock *fib;
  31.    APTR oldWindowPtr;
  32.    LONG lock, startcd;
  33.    BOOL Found, InitialCD, FullPath;
  34.    int  i;
  35.    char sbuf[SBUFSZ], cbuf[CBUFSZ];
  36.  
  37.    /* Fail if not CLI process */
  38.    proc = (struct Process *)FindTask(NULL);
  39.    cli = (struct CommandLineInterface *)(proc->pr_CLI << 2);
  40.    if(!cli)  exit(RETURN_ERROR);
  41.  
  42.    /* Allocate a FileInfoBlock - must be longword aligned */
  43.    if(!(fib=(struct FileInfoBlock *)
  44.      AllocMem(sizeof(struct FileInfoBlock),MEMF_PUBLIC|MEMF_CLEAR)))
  45.       printf("Not enough memory\n"), exit(RETURN_FAIL);
  46.  
  47.    /* Save old WindowPtr, and disable volume requesters */
  48.    oldWindowPtr = proc->pr_WindowPtr;
  49.    proc->pr_WindowPtr = (APTR)-1L;
  50.  
  51.    /* Were we given full path in command name ? */
  52.    for(FullPath = FALSE, i=0; i<strlen(command); i++)
  53.      {
  54.      if(command[i] == ':')
  55.         {
  56.         FullPath = TRUE;
  57.         break;
  58.         }
  59.      }
  60.  
  61.    /* Check current directory */
  62.    if(Found = getPath(command,fib,sbuf))
  63.       {
  64.       if((!FullPath)&&(command[0]))  strcpy(sbuf,command);
  65.       }
  66.  
  67.    /* Check paths */
  68.    if((!Found)&&(!FullPath))
  69.       {
  70.       InitialCD = TRUE;
  71.       /* Follow the BPTR path list */
  72.       for(path = (struct Path *) BADDR(cli->cli_CommandDir);
  73.           (path) && (!Found);
  74.             path = (struct Path *) BADDR(path->path_Next))
  75.          {
  76.          /* CD to each path */
  77.          lock = CurrentDir(path->path_Lock);
  78.          if(InitialCD)  startcd = lock, InitialCD = FALSE;
  79.  
  80.          /* See if command is there */
  81.          Found = getPath(command,fib,sbuf);
  82.          }
  83.       /* If we CD'd anywhere, restore initial CD */
  84.       if(! InitialCD)  CurrentDir(startcd);
  85.       }
  86.  
  87.    /* Check C: */
  88.    if((!Found)&&(!FullPath))
  89.       {
  90.       strcpy(cbuf,"C:");
  91.       strcpy(&cbuf[2],command);
  92.       if(Found = getPath(cbuf,fib,sbuf))  strcpy(sbuf,cbuf);
  93.       }
  94.  
  95.    /* Re-enable volume requesters */
  96.    proc->pr_WindowPtr = oldWindowPtr;
  97.  
  98.    /* Free fib */
  99.    if (fib)
  100.     FreeMem(fib, sizeof(struct FileInfoBlock));
  101.  
  102.    if(Found)
  103.     return(sbuf);
  104.    else
  105.     return(NULL);
  106. }
  107.  
  108.  
  109. BOOL
  110. getPath(command,fib,buf)
  111. char *command;
  112. struct FileInfoBlock *fib;
  113. char *buf;
  114.    {
  115.    LONG lock;
  116.    BOOL Success = FALSE;
  117.  
  118.    if(lock = Lock(command,ACCESS_READ))
  119.       {
  120.       if(Examine(lock,fib))
  121.          {
  122.          Success = TRUE;
  123.          buildPath(lock,fib,buf);
  124.          }
  125.       UnLock(lock);
  126.       }
  127.    return(Success);
  128.    }
  129.  
  130.  
  131. buildPath(inlock,fib,buf)
  132. LONG inlock;
  133. struct FileInfoBlock *fib;
  134. char *buf;
  135.    {
  136.    int i;
  137.    LONG lock,oldlock;
  138.    BOOL MyOldLock = FALSE;
  139.  
  140.    buf[0] = NULL;
  141.    lock = inlock;
  142.  
  143.    while(lock)
  144.       {
  145.       if(Examine(lock,fib))
  146.          {
  147.          if(fib->fib_FileName[0] > ' ')
  148.             {
  149.             if(buf[0]) insert(buf,"/");
  150.             insert(buf,fib->fib_FileName);
  151.             }
  152.          }
  153.       oldlock = lock;
  154.       lock = ParentDir(lock);
  155.       if(MyOldLock)  UnLock(oldlock);
  156.       else           MyOldLock = TRUE;
  157.       }
  158.  
  159.    if(fib->fib_FileName[0] > ' ')
  160.       {
  161.       for(i=0; i<(strlen(buf)); i++)
  162.          {
  163.          if(buf[i] == '/')
  164.             {
  165.             buf[i] = ':';
  166.             break;
  167.             }
  168.          }
  169.       }
  170.    else  insert(buf,"RAM:");
  171.  
  172.    return(strlen(buf));
  173.    }
  174.  
  175. insert(buf,s)
  176. char *buf,*s;
  177.    {
  178.    char tmp[SBUFSZ];
  179.  
  180.    strcpy(tmp,buf);
  181.    strcpy(buf,s);
  182.    strcpy(&buf[strlen(s)],tmp);
  183.    }
  184.  
  185.